package site.xuqing.autohelperengine.service;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import site.xuqing.autohelperengine.AutoHelper;
import site.xuqing.autohelperengine.StepManager;

/**
 * @author xuqing
 */
public class HelperService extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            return;
        }
        AccessibilityNodeInfo root = getRootInActiveWindow();
        if (root != null) {
            try {
                //new StepUtilImpl().printNodes(root);
                System.out.println("-----------------------------currentActivityName:"+event.getClassName());
                if (event.getClassName()!=null) {
                    StepManager.setCurrentScreenActivityClassName(event.getClassName().toString());
                    AutoHelper.getInstance().setCurrentScreenData(root,event.getClassName().toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        StepManager.setService(this);
    }

    @Override
    public void onInterrupt() {
    }
}
